package luke4fun.wheresmymoney.fragments;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;

import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import luke4fun.wheresmymoney.R;
import luke4fun.wheresmymoney.adapter.list.BasicAdapter;
import luke4fun.wheresmymoney.adapter.list.impl.StatisticLegendAdapter;
import luke4fun.wheresmymoney.bean.SummaryBean;
import luke4fun.wheresmymoney.common.CommonConstants;
import luke4fun.wheresmymoney.db.AccDBHelper;
import luke4fun.wheresmymoney.db.DBConstants;
import luke4fun.wheresmymoney.panel.StatisticsChartPanel;
import luke4fun.wheresmymoney.service.async.MonthSummaryAsyncTask;
import luke4fun.wheresmymoney.util.DateUtil;
import luke4fun.wheresmymoney.util.FormatUtil;

public class MonthStatisticFragment extends Fragment {
	
	private Context context;
	private StatisticsChartPanel chartPanel;
	private TextView dateText;
	private TextView sumText;
	private GridView gridView;
	
	private AccDBHelper dbHelper;
	private BasicAdapter<SummaryBean> adapter;

	private List<SummaryBean> dataList;
	private long dateInMillis;
	
	public static MonthStatisticFragment getInstance(long timeInMillis) {
		MonthStatisticFragment fragment = new MonthStatisticFragment();
		Bundle bundle = new Bundle();
		bundle.putLong(CommonConstants.ARG_DATE, timeInMillis);
		fragment.setArguments(bundle);
		
		return fragment;
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		context = getActivity();

		// init views
		View v = inflater.inflate(R.layout.fragment_month_statistic, container, false);
    	chartPanel = (StatisticsChartPanel) v.findViewById(R.id.chart);
    	dateText = (TextView) v.findViewById(R.id.date_text);
    	sumText = (TextView) v.findViewById(R.id.sum_text);
        gridView = (GridView) v.findViewById(R.id.color_legend_grid);
        
        if (adapter == null) {
        	adapter = new StatisticLegendAdapter(context); 
        }
        gridView.setAdapter(adapter);
        
        return v;
    }
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		dbHelper = new AccDBHelper(context);
		
//		if (savedInstanceState != null) {
//			this.dateInMillis = savedInstanceState.getLong("dateInMillis");
//		} else {
			this.dateInMillis = getArguments().getLong(CommonConstants.ARG_DATE);
//		}
	}
	
	@Override
	public void onStart() {
		super.onStart();
		setView();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		if (dbHelper != null) {
			dbHelper.close();
		}
	}
	
	private void setView() {
		setSummary();
		
		if (dataList != null) {
			updateData(dataList);
			return;
		}
		if (context != null) {
			new MonthSummaryAsyncTask(context, this, this.dateInMillis).execute();
		}
	}
	
	private void setSummary() {
		SQLiteDatabase db = dbHelper.getReadableDatabase();

		Calendar c1 = Calendar.getInstance();
		c1.clear();
		c1.setTimeInMillis(this.dateInMillis);
		
		int year = c1.get(Calendar.YEAR);
		int month = c1.get(Calendar.MONTH);
		
		dateText.setText(c1.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault()) + " " + c1.get(Calendar.YEAR));
		c1.clear();
		c1.set(year, month, 1);
		
		Cursor cursor = db.rawQuery("SELECT SUM(price) as price FROM " + DBConstants.V_RECORD + " WHERE datatime >= ? AND datatime < ? AND pid = -1",
				new String[] { c1.getTimeInMillis() + "", DateUtil.getNextMonthInMillis(c1.getTimeInMillis()) + "" });
		
		while (cursor.moveToNext()) {
			sumText.setText(""
					+ FormatUtil.formatNumber(cursor.getInt( cursor.getColumnIndex(DBConstants.RECORD_PRICE) )));
		}
		
		db.close();
	}
	
	public void updateData(List<SummaryBean> dataList) {
		this.dataList = dataList;
		adapter.updateList(dataList);
		chartPanel.setDataList(this.dataList);
		chartPanel.invalidate();
	}
	
}
